package com.mobisystems.youpark;

import java.util.Arrays;
import java.util.List;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphUser;
import com.mobisystems.youpark.utils.UtilityFunctions;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button loginButton;
	private Button registerButton;
	private Button signInWithFacebookButton;
	private UtilityFunctions utilities;
	
	// public final String facebookAppId = getResources().getString(R.string.facebookAppID);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ParseUser currentUser = ParseUser.getCurrentUser();
		Session session = ParseFacebookUtils.getSession();
		
		if(session != null && session.isOpened()) {
			makeMeRequest();
		}
		
		if (currentUser != null) {
			Intent mapIntent = new Intent(MainActivity.this, MyMap.class);
			startActivity(mapIntent);
			finish();
		} else {
			setContentView(R.layout.activity_main);
			loginButton = (Button) findViewById(R.id.loginButton);
			registerButton = (Button) findViewById(R.id.registerButton);
			

			FragmentManager fm = getFragmentManager();
			FragmentTransaction transaction = fm.beginTransaction();
			MainFragment mainFragment = new MainFragment();

			transaction.replace(R.id.fragment_placeholder, mainFragment);
			transaction.commit();

		}
	}

	private void makeMeRequest() {
		Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),
				new Request.GraphUserCallback() {
					
					@Override
					public void onCompleted(GraphUser user, Response response) {
						Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
					}
				});
		request.executeAsync();
	}

	public void onSelectFragment(View view) {
		Fragment newFragment = null;

		if (view == findViewById(R.id.loginButton)) {
			newFragment = new SignInFragment();
		} else if (view == findViewById(R.id.loginButtonFacebook)) {
			signInWithFacebookButton = (Button) findViewById(R.id.loginButtonFacebook);
			
			
			
		} else if (view == findViewById(R.id.registerButton)) {
			newFragment = new RegisterFragment();
		} else {
			newFragment = new MainFragment();
		}

		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();
		transaction.replace(R.id.fragment_placeholder, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();
	}

	public void attemptLogin(View view) {
		Intent intent = new Intent(this, MyMap.class);
		startActivity(intent);
		finish();
	}


	public void onFacebookLoginButtonClicked(View view) {
		if(view == findViewById(R.id.loginButtonFacebook)){
				List<String> permissions = Arrays.asList("public_profile", "user_about_me","user_relationships","user_birthday","user_location","user_friends");
				ParseFacebookUtils.logIn(permissions, MainActivity.this, new LogInCallback() {
					
					@Override
					public void done(ParseUser user, ParseException e) {
						if(user == null) {
							Log.d(Application.TAG, "Cancelled Login");
						} else if(user.isNew()) {
							Log.d(Application.TAG, "User signed up and logged in through facebook");
						} else {
							Log.d(Application.TAG, "User logged in through facebook");
						}
					}
				});
			}
		}
	}
